//
import router from "./router";
import store from "./store";

// 导入进度条
import nprogress from "nprogress";
import "nprogress/nprogress.css";

// 准备白名单
let whiteList = ["/login", "/404"];
router.beforeEach(async (to, from, next) => {
  nprogress.start();
  if (store.getters.token) {
    if (to.path == "/login") {
      next("/");
    } else {
      // 如果没有用户数据再发送请求
      if (!store.getters.userId) {
        await store.dispatch("user/getUserInfo");
      }

      next();
    }
  } else {
    // 没登录
    if (whiteList.includes(to.path)) {
      next();
    } else {
      next("/login");
    }
  }
});

router.afterEach(route => {
  nprogress.done();
});
